/*
Copyright 2019 The OpenEBS Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package udev

import (
	"github.com/openebs/node-disk-manager/integration_tests/utils"
	"strings"
)

// UDEV events available in the system
const (
	UdevEventAdd    = "add"
	UdevEventRemove = "remove"
)

var execCommandWithPipe = utils.ExecCommandWithPipe

// TriggerEvent triggers a udev event for the device with syspath.
// The event is generated by writing the event action into
// syspath/devicename/uevent file. This write will cause UDEV to trigger
// the corresponding event. It is same as using `udevadm trigger`
func TriggerEvent(event, syspath, devicePath string) error {
	splitName := strings.Split(devicePath, "/")
	deviceName := splitName[len(splitName)-1]
	fileName := syspath + "/" + deviceName + "/uevent"
	command1 := "echo " + event
	command2 := "sudo tee " + fileName
	_, err := execCommandWithPipe(command1, command2)
	return err
}
